package com.wing.bill.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.model.entity.WarehouseProcessBill;
import com.wing.bill.model.response.WarehouseProcessBillVO;
import com.wing.bill.service.WarehouseProcessBillService;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(value = "生产加工账单管理", tags = "#/warehouse/bill/warehouseProcessBill")
@RestController("WarehouseProcessBillController")
@RequestMapping("/warehouse/admin/processBill")
@RequiredArgsConstructor
public class WarehouseProcessBillController {

    @Autowired
    private WarehouseProcessBillService warehouseProcessBillService;

    @ApiOperation(value = "查看账单", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProcessBillVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String warehouseCustomerId
            ,String billPeriod
            ,String billStatus
    ){
        Page<WarehouseProcessBill> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        //判断客户端，如果是会员端，则需要添加对应的部门信息作为查询条件
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            paramMap.put("customerId",JwtUtils.getDepartmentId());
        }else{
            if(!StringUtils.isEmpty(warehouseCustomerId)){
                paramMap.put("customerId",warehouseCustomerId);
            }
        }
        if(!StringUtils.isNotEmpty(billPeriod)){
            paramMap.put("billPeriod",billPeriod);
        }
        if(!StringUtils.isNotEmpty(billStatus)){
            paramMap.put("billStatus",billStatus);
        }
        //搜索
        IPage<WarehouseProcessBillVO> list = warehouseProcessBillService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "结算", notes = "结算")
    @GetMapping("/settlement")
    public JsonResult settlement(Long id, Double amount, String billVoucherUrl){
        WarehouseProcessBill warehouseProcessBill =  warehouseProcessBillService.settlement(id, amount,billVoucherUrl);
        return JsonResult.success(warehouseProcessBill);
    }

    @ApiOperation(value = "查询账单详情", notes = "查询")
    @GetMapping ("/selectBill")
    public JsonResult<WarehouseProcessBillVO> selectBill(@RequestParam String id){
        WarehouseProcessBillVO warehouseProcessBillVO = warehouseProcessBillService.selectBill(id);
        return JsonResult.success(warehouseProcessBillVO);
    }
}
